package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> list) {
            //判断当前对象是否进行了预约设置
            //
           /* for (OrderSetting orderSetting : list) {
                Date orderDate = orderSetting.getOrderDate();
                if (orderSettingDao.findCountByDate(orderDate) > 0) {
                    orderSettingDao.editOrderSettingByDate(orderSetting);
                    continue;   //跳过当前这次循环中continue语句后尚未执行的语句
                }
                orderSettingDao.add(orderSetting);
            }*/
        for (OrderSetting orderSetting : list) {
            Date orderDate = orderSetting.getOrderDate();
            OrderSetting order = orderSettingDao.findOrdersettingByDate(orderDate);
            if (order !=null) { //判断是否为空
                int number = order.getNumber();
                if (number != orderSetting.getNumber()) { //不为空判断number是否一致
                    orderSettingDao.editOrderSettingByDate(orderSetting);
                }
                   //跳过当前这次循环中continue语句后尚未执行的语句
                //一致直接跳过
                continue;
            }
            //为空直接insert
            orderSettingDao.add(orderSetting);
        }


    }

    @Override
    public Result getOrdersettingByMonth(String date) {

        try {
            String start = date + "-1";
            String end = date + "-31";
            HashMap map = new HashMap();
            map.put("dateStart", start);
            map.put("dateEnd", end);
            List<Map> ordersettingList = orderSettingDao.getOrdersettingByMonth(map);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,ordersettingList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @Override
    public Result editNumberByDate(OrderSetting orderSetting) {

        try {
            //判断这个orderSetting对象就是该日期是否已经存在
            if (orderSettingDao.findCountByDate(orderSetting.getOrderDate()) > 0) {
                orderSettingDao.editOrderSettingByDate(orderSetting);
            } else {
                orderSettingDao.add(orderSetting);
            }
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
